package net.spider.analyze.impl;

import java.io.IOException;
import java.util.Date;

import net.cl.support.json.JsonArray;
import net.cl.support.json.JsonException;
import net.cl.support.json.JsonObject;
import net.cl.utils.HttpUtil;
import net.spider.analyze.Analyzable;
import net.spider.analyze.VideoAnalyze;

public class Youku implements Analyzable, VideoAnalyze {

	private String title;
	private String thumbnail;

	public Youku() {

	}

	public String genKey(String key1, String key2) {
		int key = Long.valueOf(key1, 16).intValue();
		key ^= 0xA55AA5A5;
		return key2 + Long.toHexString(key);
	}

	public String mkInfoURL(String youkuId) {
		return "http://v.youku.com/player/getPlayList/VideoIDS/" + youkuId;
	}

	public String mkFlvPath(int seg, String type, String sid, String fileid, String key) {
		String n = Integer.toHexString(seg);
		while (n.length() < 2)
			n = '0' + n;
		String s = String.format("http://f.youku.com/player/getFlvPath/sid/%s_%s/st/%s/fileid/%s%s%s?K=%s", sid, n,
				type, fileid.substring(0, 8), n, fileid.substring(10), key);
		return s;
	}

	public StringBuilder getFileIDMixString(Double seed) {
		StringBuilder mixed = new StringBuilder();
		StringBuilder source = new StringBuilder(
				"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890");
		int index, len = source.length();
		for (int i = 0; i < len; ++i) {
			seed = (seed * 211 + 30031) % 65536;
			index = (int) Math.floor(seed / 65536 * source.length());
			mixed.append(source.charAt(index));
			source.deleteCharAt(index);
		}
		return mixed;
	}

	public String getFileID(String fileid, Double seed) {
		StringBuilder mixed = getFileIDMixString(seed);
		String[] ids = fileid.split("\\*");
		StringBuilder realId = new StringBuilder();
		int idx;
		for (int i = 0; i < ids.length; i++) {
			idx = Integer.parseInt(ids[i]);
			realId.append(mixed.charAt(idx));
		}
		return realId.toString();
	}

	public static void main(String[] args) {
		try {
			Youku y = new Youku();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Youku analyze() {
		// System.out.println(Long.valueOf("b14af87d", 16));
		String sid = String.format("%d%d%d", System.currentTimeMillis(),
				(int) (1000 + Math.floor(Math.random() * 999)), (int) (1000 + Math.floor(Math.random() * 9000)));
		String url = mkInfoURL("XMjE1NDAwMDEy");
		String resp = HttpUtil.getResponseString(url);
		try {
			JsonObject jsObj = JsonObject.fromString(resp).getJsonArray("data").getJsonObject(0);
			title = jsObj.getString("title");
			String thumbnail1 = jsObj.getString("logo");
			thumbnail = thumbnail1;
			double seed = jsObj.getDouble("seed");
			String key1 = jsObj.getString("key1");
			String key2 = jsObj.getString("key2");
			// System.out.println(key1);
			// System.out.println(key2);
			String fileids = jsObj.getJsonObject("streamfileids").getString("flv");
			// System.out.println(fileids);
			String streamtype = jsObj.getJsonArray("streamtypes").getString(0);
			JsonArray segs = jsObj.getJsonObject("segs").getJsonArray("flv");
			// System.out.println(streamtype);
			JsonArray tags = jsObj.getJsonArray("tags");
			// for (int i = 0; i < tags.size(); i++) {
			// System.out.println(tags.getString(i));
			// }
			String fileid = getFileID(fileids, seed);
			String key = genKey(key1, key2);
			// System.out.println(jsObj.getString("title"));
			for (int i = 0; i < segs.size(); i++) {
				// System.out.println(mkFlvPath(segs.getJSONObject(i).getInt("no"),
				// streamtype, sid, fileid, key));
			}
		} catch (JsonException e) {
			e.printStackTrace();
		}
		return this;
	}

	@Override
	public String getThumbnail() {
		// TODO Auto-generated method stub
		return thumbnail;
	}

	@Override
	public String getVideoSource() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		return title;
	}

	@Override
	public String getAuthor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Date getPubDate() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getText() {
		// TODO Auto-generated method stub
		return null;
	}

}
